package com.welink.cmp.adapter.api.metadata;

import com.welink.base.exception.BizException;
import com.welink.cmp.dto.ErrorCode;
import com.welink.cmp.dto.metadata.DestroyUpdateCmd;
import com.welink.cmp.dto.metadata.host.*;
import com.welink.cmp.meatdata.service.HostAppService;
import com.welink.common.dto.Response;
import com.welink.common.dto.SingleResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 主机生命周期管理接口
 *
 * @author liyanwei
 */
@RestController
@RequestMapping("/api/host")
public class HostLifeCycleServiceApi {

    @Autowired
    private HostAppService hostAppService;

    /**
     * 创建主机
     * 【状态机：购买】
     * @param cmd 创建主机指令
     * @return 主机ID
     */
    @PostMapping("/addHost")
    public SingleResponse<Long> addHost(@RequestBody CreateHostAddCmd cmd) {
        final Long id = hostAppService.addHost(cmd);
        if (id == null){
            return SingleResponse.buildFailure(ErrorCode.CommonError.E_Service_Inner_Exception,"数据保存失败");
        }
        return SingleResponse.of(id);
    }

    /**
     * 配置主机基础信息
     *
     * @param cmd 配置主机基础信息指令
     * @return 操作结果
     */
    @PostMapping("/setup/hostBase")
    public Response setupHostBaseInfo(@RequestBody SetupHostBaseUpdateCmd cmd) {
        final Boolean b = hostAppService.setupHostBase(cmd);
        if (!b){
            throw new BizException("配置主机基础信息异常");
        }
        return Response.buildSuccess();
    }

    /**
     * 配置主机网络信息
     *
     * @param cmd 配置主机网络信息指令
     * @return 操作结果
     */
    @PostMapping("/setup/hostNet")
    public Response setupHostNetInfo(@RequestBody SetupHostNetUpdateCmd cmd) {
        final Boolean b = hostAppService.setupHostNet(cmd);
        if (!b){
            throw new BizException("配置主机基础信息异常");
        }
        return Response.buildSuccess();
    }

    /**
     * 配置主机物理机房信息
     *
     * @param cmd 配置主机物理机房信息指令
     * @return 操作结果
     */
    @PostMapping("/setup/hostRack")
    public Response setupHostRackInfo(@RequestBody SetupHostRackUpdateCmd cmd) {
        final Boolean b = hostAppService.setupHostDc(cmd);
        if (!b){
            throw new BizException("配置主机基础信息异常");
        }
        return Response.buildSuccess();
    }

    /**
     * 上架主机
     *
     * @param cmd 上架主机指令
     * @return 操作结果
     */
    @PostMapping("/launchHost")
    public Response launchHost(@RequestBody LaunchHostUpdateCmd cmd) {
        final Boolean b = hostAppService.launchHost(cmd);
        if (!b){
            throw new BizException("上架主机指令异常");
        }
        return Response.buildSuccess();
    }



    /**
     * 下架主机
     *
     * @param cmd 下架主机指令
     * @return 操作结果
     */
    @PostMapping("/shutdownHost")
    public Response shutdownHost(@RequestBody ShutdownHostUpdateCmd cmd) {
        return hostAppService.shutdownHost(cmd);
    }

    /**
     * 注销主机
     *
     * @param cmd 注销主机指令
     * @return 操作结果
     */
    @PostMapping("/destroyHost")
    public Response destroyHost(@RequestBody DestroyUpdateCmd cmd) {
        return hostAppService.destroyHost(cmd);
    }


}